package com.taptech.doufu.ui.activity;

import android.os.Bundle;
import android.view.View;

import com.taptech.doufu.R;
import com.taptech.doufu.bean.personalcenter.PersonalNotifyBean;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.listener.HttpResponseListener;
import com.taptech.doufu.net.httputils.HttpResponseObject;
import com.taptech.doufu.services.personalcenter.PersonalInfoService;
import com.taptech.doufu.ui.activity.base.DiaobaoBaseActivity;
import com.taptech.doufu.ui.adapter.personalcenter.PersonalNotifyAdapter;
import com.taptech.doufu.ui.view.PullToRefreshListView;
import com.taptech.doufu.ui.view.PullToRefreshListView.OnLoadAndRefreshListener;
import com.taptech.doufu.util.DiaobaoUtil;
import com.taptech.doufu.util.UIUtil;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.List;

/**
 * <p>
 * Company: DiaoBao
 * </p>
 *
 * @author huangzh
 * @date 2014-3-20
 */
public class PersonalNotifyActivity extends DiaobaoBaseActivity implements HttpResponseListener, OnLoadAndRefreshListener {

    private PullToRefreshListView notifys;
    private PersonalNotifyAdapter adapter;
    private String last;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.personal_center_activity_notify);
        initView();
    }

    private void initView() {
        notifys = (PullToRefreshListView) findViewById(R.id.personal_activity_notify_list);
        // notifys.setRefreshable(true);
        notifys.setOnLoadAndRefreshListener(this);
        adapter = new PersonalNotifyAdapter(this);
        notifys.setAdapter(adapter);
    }

    @SuppressWarnings("unchecked")
    @Override
    public void handleResponse(int handleType, HttpResponseObject response) {
        try {
            if (response.getStatus() == Constant.STATUS_SUCCESS) {
                PersonalInfoService.getInstance().getUnreadInfo().setNotify("0");
                PersonalInfoService.getInstance().refreshUnreadInfo();
                JSONArray datas = ((JSONObject) response.getData()).getJSONArray("main");
                List<PersonalNotifyBean> notifys = DiaobaoUtil.jsonArray2BeanList(PersonalNotifyBean.class, datas);
                if (handleType == PersonalInfoService.HANDLE_LOAD_NOFITYS) {
                    if (datas.length() == Constant.COMMENT_SIZE) {
                        this.notifys.setLoadmoreable(true);
                    }
                    if (datas.length() == 0) {
                        this.notifys.setFull(true);
                        this.notifys.loadMoreComplete();
                    }
                    adapter.setSuperDatas(notifys);
                } else if (handleType == PersonalInfoService.HANDLE_REFRESH_NOFITYS) {
                    this.notifys.onRefreshComplete();
                    adapter.setDataSource(notifys);
                }
                last = ((JSONObject) response.getData()).getString(Constant.LAST);
            } else {
                UIUtil.toastMessage(this, DiaobaoUtil.getErrorTips(response));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus && adapter.getDataSource() == null) {
            PersonalInfoService.getInstance().loadNofifys("0", -1, this);
        }
    }

    public void back(View view) {
        onBackPressed();
    }

    @Override
    public void finish() {
        super.finish();
    }

    @Override
    public void onRefresh() {
        PersonalInfoService.getInstance().loadNofifys(last, 0, this);

    }

    @Override
    public void loadMore() {
        PersonalInfoService.getInstance().loadNofifys(last, -1, this);
    }

}
